#ifndef __CAMERA_H
#define __CAMERA_H

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

/*
	CAMERA.H

	The interface for CCamera - the camera system

	Author: Kevin Hawkins
	Date: 3/29/2001
	Description: The CCamera class is the camera system

*/

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "vector.h"

class CObject;
class CWorld;
class CTerrain;

// g_fFovY is defined in "oglwindow.cpp"
extern const GLfloat g_fFovY;

class CCamera
{
private:
	// these are used for moving and changing camera orientation
	// through the MoveTo/LookTo methods
	CVector initPosition, finalPosition;
	CVector initLookAt, finalLookAt;

	CVector lookAtVel;			// velocity for looking at objects
	CVector lookAtAccel;		// acceleration for looking at objects

	void UpdateLookAt();
	void UpdateMoveTo();

	//bool m_telescope;
	float m_speedScale;
	static int planes[4]; // plane

public:
	CVector position;			// position of camera
	CVector velocity;			// velocity of camera
	CVector acceleration;		// acceleration of camera
	CVector lookAt;			// lookat vector

	// up, forward, right vectors
	CVector up;
	CVector forward;
	CVector right;

	// yaw and pitch angles
	float yaw;
	float pitch;

	int screenWidth, screenHeight;
	int centerX, centerY;

	// bottom, right, top, left clip plane normals
	CVector clipNormmals[4];

	CWorld * world;
	CTerrain * terrain;

	CCamera();
	CCamera(int width, int height) { /*m_telescope = false;*/m_speedScale = 1.0f;}
	CCamera(CVector *look);
	CCamera(CVector *pos, CVector *look);
	~CCamera();

	//void SetTelescope(bool state);
	//bool UsingTelescope();

	void LookAt(CObject *object);
	void LookAtNow(CObject *object);
	void MoveTo(CObject *object);
	void MoveToNow(CObject *object);
	void MoveToNow(scalar_t x, scalar_t y, scalar_t z);

	// right rotation along y-axis (yaw)
	void RotateYaw(scalar_t radians);
	void RotatePitch(scalar_t radians);
	void RotateRoll(scalar_t radians);

	// do physics calculations
	void Animate(scalar_t deltaTime);
	void SetRunState(bool);

	inline void CalcClipNormals();
	inline BOOL VisibleOfPoint(const CVector & pt);
	inline BOOL VisibleOfPointEx(const CVector & pt, const float & dis);
	inline BOOL VisibleOfTriangle(const CVector & v1, const CVector & v2, const CVector & v3);
	inline int VisibleOfSphere(const CVector & center, const float & r);
};

//extern const GLfloat g_fFovY;
extern GLfloat g_fAspect;

/*
==============================================
*/

void CCamera::CalcClipNormals()
{
	CVector n(lookAt.x - position.x, lookAt.y - position.y, lookAt.z - position.z);
	n.Normalize();
	CVector v(0.0f, 1.0f, 0.0f);
	CVector u;
	CrossProduct(n, v, u); // u = cross(n, v)
	CrossProduct(u, n, v); // v = cross(u, n)
	u.Normalize();
	v.Normalize();

	GLfloat h = tan((GLfloat)(g_fFovY * 3.1415926535f /360.0f));
	GLfloat w = g_fAspect * h;

	/*
	3....2
	.    .
	.    .
	0....1
	*/

	CVector v0(u*(-w) + v*(-h) + n);
	CVector v1(u*w    + v*(-h) + n);
	CVector v2(u*w    + v*h    + n);
	CVector v3(u*(-w) + v*h    + n);

	CrossProduct(v1, v0, clipNormmals[0]);
	CrossProduct(v2, v1, clipNormmals[1]);
	CrossProduct(v3, v2, clipNormmals[2]);
	CrossProduct(v0, v3, clipNormmals[3]);

	clipNormmals[0].Normalize();
	clipNormmals[1].Normalize();
	clipNormmals[2].Normalize();
	clipNormmals[3].Normalize();

	return;
}

inline BOOL CCamera::VisibleOfPoint(const CVector & pt)
{
	CVector t(pt - position);

	for (int i = 0; i < sizeof(planes)/sizeof(planes[0]); ++i)
	{
		if (t.DotProduct(clipNormmals[planes[i]]) < 0)
			return FALSE;
	}
	return TRUE;
}

inline BOOL CCamera::VisibleOfTriangle(const CVector & v1, const CVector & v2, const CVector & v3)
{
	if (VisibleOfPoint(v1) || VisibleOfPoint(v2) || VisibleOfPoint(v3))
	{
		return TRUE;
	}
	return FALSE;
}

inline BOOL CCamera::VisibleOfSphere(const CVector & center, const float & r)
{
	CVector t;

	for (int i = 0; i < sizeof(planes) / sizeof(planes[0]); ++i)
	{
		t = center - (position - clipNormmals[planes[i]] * r);
		if (t.DotProduct(clipNormmals[planes[i]]) < 0)
			return FALSE;
	}
	return TRUE;
}

#endif
